from crawler.extractors import JsonpExtractor
from crawler.fields import JsonField, ContainerField

__all__ = ['LOUMainExtractorMainInfoExtractor']


class LOUMainExtractorMainInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.hits[*]')
    spu_id = JsonField(json_selector='$.productId')
    spu_num = JsonField(json_selector='$.identifier')
    title = JsonField(json_selector='$.name')
    color = JsonField(json_selector='$.color')
    url = JsonField(json_selector='$.url')
    size = JsonField(json_selector='$.size')
    # 到这里了
    price = JsonField(json_selector='$.offers.priceSpecification[0].price')


class LOUMainExtractor:
    @classmethod
    def get_item(cls, response):
        return cls._extract(response)

    @classmethod
    def _extract(cls, response):
        task = response.meta['task']
        sex = task['info']['sex']
        category = task['info']['category']
        info_list = list()
        for item in LOUMainExtractorMainInfoExtractor.get_items(response):
            info_dict = dict()
            # info_dict['spu_id'] = item.spu_id
            # 货号
            info_dict['spu_id'] = item.spu_num
            # 商标题
            info_dict['title'] = item.title
            info_dict['size'] = item.size if item.size else ""
            info_dict['brand'] = 'Lv'
            info_dict['sex'] = sex
            info_dict['category'] = category
            # 品类
            info_dict['color'] = item.color
            info_dict['cost_price'] = item.price.replace('xa0', '').replace('£', '')
            info_dict['retail_price'] = info_dict['cost_price']
            info_dict['currency'] = "£"
            info_dict['url'] = item.url

            info_list.append(info_dict)

        return info_list
